#!/bin/bash
set -e
PATH=$PATH:/usr/lib/jvm/default-jvm/bin/
export PATH

cd /data/amdb

# jar -xvf amdb-app.jar BOOT-INF/classes/application-shard.yml
# jar -xvf amdb-app.jar BOOT-INF/classes/shard.properties
jar -xvf amdb-app.jar BOOT-INF/classes/generator.properties
# sed -i -e "s/userName: default/userName: $CLICKHOUSE_USERNAME/g" BOOT-INF/classes/application-shard.yml
# sed -i -e "s/password: rU4zGjA\//password: $CLICKHOUSE_PASSWORD/g" BOOT-INF/classes/application-shard.yml
# sed -i -e "s/username: \${resource.mysql.password}/username: \${resource.mysql.username}/g" BOOT-INF/classes/application-shard.yml
# sed -i -e "s/password: \${resource.mysql.username}/password: \${resource.mysql.password}/g" BOOT-INF/classes/application-shard.yml

# sed -i -e "s/resource.mysql.host=.*/resource.mysql.host=$MYSQL_HOST/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/resource.mysql.port=.*/resource.mysql.port=$MYSQL_PORT/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/resource.influxdb.host=.*/resource.influxdb.host=$INFLUXDB_HOST/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/resource.influxdb.port=.*/resource.influxdb.port=$INFLUXDB_PORT/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/amdb.out.url.ip=.*/amdb.out.url.ip=$AMDB_HOST/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/surge.config.data.zk.servers=.*/surge.config.data.zk.servers=$ZK_HOSTS/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/tro-web.out.url.host=.*/tro-web.out.url.host=$TRO_WEB_HOST/g" BOOT-INF/classes/shard.properties
# sed -i -e "s/tro-web.out.url.port=.*/tro-web.out.url.port=$TRO_WEB_PORT/g" BOOT-INF/classes/shard.properties
sed -i -e "s/jdbc.generator.url=.*/jdbc.generator.url=jdbc:mysql:\/\/$MYSQL_HOST:$MYSQL_PORT\/amdb/g" BOOT-INF/classes/generator.properties
sed -i -e "s/jdbc.generator.username=.*/jdbc.generator.username=$MYSQL_USERNAME/g" BOOT-INF/classes/generator.properties
sed -i -e "s/jdbc.generator.password=.*/jdbc.generator.password=$MYSQL_PASSWORD/g" BOOT-INF/classes/generator.properties

# echo resource.mysql.username=$MYSQL_USERNAME >> BOOT-INF/classes/shard.properties
# echo resource.mysql.password=$MYSQL_PASSWORD >> BOOT-INF/classes/shard.properties
# echo resource.influxdb.username=$INFLUXDB_USERNAME >> BOOT-INF/classes/shard.properties
# echo resource.influxdb.password=$INFLUXDB_PASSWORD >> BOOT-INF/classes/shard.properties
# echo resource.clickhouse.url=jdbc:clickhouse://$CLICKHOUSE_HOST/default >> BOOT-INF/classes/shard.properties

envsubst < application.yml > BOOT-INF/classes/application.yml
# cat BOOT-INF/classes/shard.properties
cat BOOT-INF/classes/application.yml
cat BOOT-INF/classes/generator.properties

jar -uvf amdb-app.jar BOOT-INF/classes/application.yml
# jar -uvf amdb-app.jar BOOT-INF/classes/application-shard.yml
# jar -uvf amdb-app.jar BOOT-INF/classes/shard.properties
jar -uvf amdb-app.jar BOOT-INF/classes/generator.properties

if [[ ! -z "$MYSQL_HOST" && ! -z "$MYSQL_PORT" ]]; then
	params="$params --resource.mysql.host=$MYSQL_HOST"
	params="$params --resource.mysql.port=$MYSQL_PORT"
fi

if [ ! -z "$MYSQL_USERNAME" ]; then
	params="$params --resource.mysql.password=$MYSQL_USERNAME"
fi

if [ ! -z "$MYSQL_PASSWORD" ]; then
	params="$params --resource.mysql.username=$MYSQL_PASSWORD"
fi

if [[ ! -z "$INFLUXDB_HOST" && ! -z "$INFLUXDB_PORT" ]]; then
	params="$params --resource.influxdb.host=$INFLUXDB_HOST"
	params="$params --resource.influxdb.port=$INFLUXDB_PORT"
fi

if [ ! -z "$INFLUXDB_USERNAME" ]; then
	params="$params --resource.influxdb.username=$INFLUXDB_USERNAME"
fi

if [ ! -z "$INFLUXDB_PASSWORD" ]; then
	params="$params --resource.influxdb.password=$INFLUXDB_PASSWORD"
fi

if [ ! -z "$ZK_HOSTS" ]; then
	params="$params --surge.config.data.zk.servers=$ZK_HOSTS"
fi

if [[ ! -z "$CLICKHOUSE_HOST" ]]; then
	params="$params --resource.clickhouse.url=jdbc:clickhouse://$CLICKHOUSE_HOST/default"
fi

if [ ! -z "$CLICKHOUSE_USERNAME" ]; then
	params="$params --resource.clickhouse.username=$CLICKHOUSE_USERNAME"
fi

if [ ! -z "$CLICKHOUSE_PASSWORD" ]; then
	params="$params --resource.clickhouse.password=$CLICKHOUSE_PASSWORD"
fi

# /wait-for.sh $ZK_HOSTS -t 60
/wait-for.sh $MYSQL_HOST:$MYSQL_PORT -t 60
/wait-for.sh $INFLUXDB_HOST:$INFLUXDB_PORT -t 60
# /wait-for.sh $CLICKHOUSE_HOST -t 60

# java -Xms512m -Xmx512m -Xss1m -jar /data/amdb/amdb-app.jar $params --datasource.traceAll=mysql 
java -XX:MaxRAMPercentage=90.0 -Xmx$MEMORY -Xms$MEMORY -Xss1m -jar /data/amdb/amdb-app.jar --datasource.traceAll=$SOURCE_TYPE 